/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Helper.WeekHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author KIENLT
 */
public class WeekDao {
    public static ArrayList<WeekHelper> FillAll(Connection cnn) throws SQLException{
        ArrayList<WeekHelper> arrWeekList = new ArrayList<WeekHelper>();
        WeekHelper weekData = null;
        String strSQL = "SELECT * FROM tblWeek";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            weekData = new WeekHelper();
            weekData.setWeekId(rs.getString("WeekId"));
            weekData.setDateBegin(rs.getString("DateBegin"));
            weekData.setDateEnd(rs.getString("DateEnd"));
            arrWeekList.add(weekData);
        }
        return arrWeekList;
    }
    
    public static int Insert(Connection cnn, WeekHelper obj) throws SQLException {
        String strSQL = "INSERT INTO tblWeek(DateBegin, DateEnd) VALUES(?,?)";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getDateBegin());
        stmt.setString(2, obj.getDateEnd());
        return stmt.executeUpdate();
    }
    
    public static int Update(Connection cnn, WeekHelper obj) throws SQLException {
        String strSQL = "UPDATE tblWeek SET DateBegin=?, DateEnd=? WHERE WeekId = ?";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getDateBegin());
        stmt.setString(2, obj.getDateEnd());
        stmt.setString(3, obj.getWeekId());
        return stmt.executeUpdate();
    }
    
    public static int Delete(Connection cnn, WeekHelper obj) throws SQLException {
        String strSQL = "DELETE FROM tblWeek WHERE WeekId = ?";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getWeekId());
        return stmt.executeUpdate();
    }
    
    public static ResultSet FillOne(Connection cnn, WeekHelper obj) throws SQLException{
        String strSQL = "SELECT * FROM tblWeek WHERE WeekId = ?";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getWeekId());
        return stmt.executeQuery();
    }
}
